﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebLibSolPedBE;
using LibSolPedI;
using LibSolPedDALF;
using AjaxControlToolkit;

namespace WebApplication1
{
    public partial class Site1 : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //asignar nuevo id de conexión
            SAPUserBE pSAPUser = new SAPUserBE();
            RfcConfigParametersBE pUsuarioWeb = new RfcConfigParametersBE();
            if (Session["objUser"] != null)
            {
                pUsuarioWeb = (RfcConfigParametersBE)Session["objUser"];    
            }
            else
            {
                Response.Redirect("credencial.aspx");
            }
            //pUsuarioWeb.Id = (Guid.NewGuid()).ToString();
            //pUsuarioWeb.UsuarioWEB = "LZAPATER";
            //pUsuarioWeb.PasswordWEB = "libertador01";
            //pUsuarioWeb.Uname = "LZAPATER";
            //pUsuarioWeb.Passwd = "libertador01";

            
        }

        protected void btnLiberar_Click(object sender, EventArgs e)
        {
            String ret = "";
            Session["mensaje"] = "";
            try
            {
                if (Session["nombreApp"].Equals("libsolped"))
                {
                    List<SolPedBE> lstSe = (List<SolPedBE>)Session["lstSolped"];
                    List<SolPedBE> lst = new List<SolPedBE>();
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            SolPedBE it = new SolPedBE();
                            it = lstSe[i];
                            lst.Add(it);
                        }
                    }
                    ret = DaoFactory.LibSolPedF.setLibSolPed(lst);
                }
                else if (Session["nombreApp"].Equals("libpedidocompra"))
                {
                    List<PedidoCompraCabBE> lstPC = (List<PedidoCompraCabBE>)Session["lstPedCom"];
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            PedidoCompraCabBE it = new PedidoCompraCabBE();
                            it = lstPC[i];
                            ret += DaoFactory.LiberaPedidoCompraF.setLiberaPedidoCompra(it.PO_NUMBER);
                        }
                    }
                }
                else if (Session["nombreApp"].Equals("libreserva"))
                {
                    List<ReservaBE> lstPC = (List<ReservaBE>)Session["lstReserva"];
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            ReservaBE it = new ReservaBE();
                            it = lstPC[i];
                            ret += DaoFactory.LiberaReservaF.setLiberaReserva(it.RSNUM, it.RSPOS);
                        }
                    }
                }
                else if (Session["nombreApp"].Equals("libpedidoabierto"))
                {
                    List<PedidoAbiertoCabBE> lstPC = (List<PedidoAbiertoCabBE>)Session["lstPA"];
                    List<PedidoAbiertoCabBE> lst = new List<PedidoAbiertoCabBE>();
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            PedidoAbiertoCabBE it = new PedidoAbiertoCabBE();
                            it = lstPC[i];
                            lst.Add(it);
                        }
                    }
                    ret = DaoFactory.PedidoAbiertoF.setLstPedidoAbiertoDet(lst);
                    
                }
                else if (Session["nombreApp"].Equals("solCajaChica"))
                {
                    List<SolCajaChicaBE> lstPC = (List<SolCajaChicaBE>)Session["lstCajaChica"];
                    List<SolCajaChicaBE> lst = new List<SolCajaChicaBE>();
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            SolCajaChicaBE it = new SolCajaChicaBE();
                            it = lstPC[i];
                            lst.Add(it);
                        }
                    }
                    ret = DaoFactory.SolCajaChicaF.setAprobarSolCajaChica(lst);
                }
                else if (Session["nombreApp"].Equals("solAnticipo"))
                {
                    List<SolAnticipoBE> lstPC = (List<SolAnticipoBE>)Session["lstAnticipo"];
                    List<SolAnticipoBE> lst = new List<SolAnticipoBE>();
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            SolAnticipoBE it = new SolAnticipoBE();
                            it = lstPC[i];
                            lst.Add(it);
                        }
                    }
                    ret = DaoFactory.SolAnticipoF.setAprobarSolAnticipo(lst);
                }
                else if (Session["nombreApp"].Equals("docSOC"))
                {
                    List<DocSOCompraBE> lstPC = (List<DocSOCompraBE>)Session["lstDocSOC"];
                    List<DocSOCompraBE> lst = new List<DocSOCompraBE>();
                    GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                    for (int i = 0; i < gv.Rows.Count; i++)
                    {
                        CheckBox chkb = (CheckBox)gv.Rows[i].Cells[0].FindControl("chk");
                        if (chkb.Checked)
                        {
                            DocSOCompraBE it = new DocSOCompraBE();
                            it = lstPC[i];
                            lst.Add(it);
                        }
                    }
                    ret = DaoFactory.DocSOCompraF.setAprobarDocSOCompra(lst);
                }
                Session.Add("accion", "R");
                Session.Add("mensaje", ret);
                
               
                Label lblMensajes = ContentPlaceHolder1.FindControl("lblMensajes") as Label;
                lblMensajes.Text = Session["mensaje"].ToString();
                ModalPopupExtender mpe = ContentPlaceHolder1.FindControl("ModalPopupExtender1") as ModalPopupExtender;
                mpe.Show();
            }
            catch (Exception ex)
            { 
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if (Session["nombreApp"].Equals("libsolped"))
            {
                List<SolPedBE> lst = LibSolPedDALF.DaoFactory.LibSolPedF.getLstSolPed();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session.Add("lstSolped", lst);
                Response.Redirect("index.aspx");
            }
            else if (Session["nombreApp"].Equals("libpedidocompra"))
            {
                List<PedidoCompraCabBE> lst = LibSolPedDALF.DaoFactory.LiberaPedidoCompraF.getLstPedidoCompraCab();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session["lstPedCom"] = lst;
                Response.Redirect("formPedidoCompraLista.aspx");
            }
            else if (Session["nombreApp"].Equals("libreserva"))
            {
                List<ReservaBE> lst = LibSolPedDALF.DaoFactory.LiberaReservaF.getLstReserva();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session["lstReserva"] = lst;
                Response.Redirect("formReservaLista.aspx");
            }
            else if (Session["nombreApp"].Equals("libpedidoabierto"))
            {
                List<PedidoAbiertoCabBE> lst = LibSolPedDALF.DaoFactory.PedidoAbiertoF.getLstPedidoAbiertoCab();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session["lstPA"] = lst;
                Response.Redirect("formPedidoAbiertoCab.aspx");
            }
            else if (Session["nombreApp"].Equals("solCajaChica"))
            {
                List<SolCajaChicaBE> lst = LibSolPedDALF.DaoFactory.SolCajaChicaF.getLstSolCajaChica();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session["lstCajaChica"] = lst;
                Response.Redirect("formSolicitudCajaChicaCab.aspx");
            }
            else if (Session["nombreApp"].Equals("solAnticipo"))
            {
                List<SolAnticipoBE> lst = LibSolPedDALF.DaoFactory.SolAnticipoF.getLstSolAnticipo();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session["lstDocSOC"] = lst;
                Response.Redirect("formSolicitudAnticipoCab.aspx");
            }
            else if (Session["nombreApp"].Equals("docSOC"))
            {
                List<DocSOCompraBE> lst = LibSolPedDALF.DaoFactory.DocSOCompraF.getLstDocSOCompra();
                GridView gv = ContentPlaceHolder1.FindControl("gvGrid") as GridView;
                gv.DataSource = lst;
                gv.DataBind();
                Session["lstAnticipo"] = lst;
                Response.Redirect("formDocSOCompra.aspx");
            }
        }

        protected void btnConfigurar_Click(object sender, EventArgs e)
        {
            Response.Redirect("credencial.aspx");
        }

        protected void ibtnHome_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("formContenedorApps.aspx");
        }
    }
}